<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="/js/include.js" type="text/javascript" charset="utf-8"></script>
    <style>
        .layui-upload-img {
            width: 30%;
            height: 30%;
            margin: 0 10px 10px 0;
        }
    </style>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>数据源详情</legend>
</fieldset>

<form class="layui-form" id="from1" action="">
    <input type="hidden" name="id">
    <div class="layui-form-item">
        <label class="layui-form-label">数据源名称</label>
        <div class="layui-input-block">
            <input type="text" name="data_name"  placeholder="请输入数据源名称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">数据库用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username"  placeholder="请输入数据库用户名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">数据库密码</label>
        <div class="layui-input-block">
            <input type="text" name="password"  placeholder="请输入数据库密码" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">数据库url</label>
        <div class="layui-input-block">
            <input type="text" name="url"  placeholder="请输入数据库url" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">数据库驱动名</label>
        <div class="layui-input-block">
            <!--<input type="text" name="driver_class_name"  placeholder="请输入数据库驱动名" class="layui-input">-->
            <select    name="driver_class_name" lay-verify="required"  >
                <option value="org.sqlite.JDBC" selected="">org.sqlite.JDBC</option>
                <option value="com.mysql.jdbc.Driver">com.mysql.jdbc.Driver</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">数据库类型</label>
        <div class="layui-input-block">

            <select   id="data_type" name="data_type" lay-verify="test789"  >
                <option value="mysql" selected="">mysql</option>
                <option value="sqlite">sqlite</option>
            </select>
        </div>
    </div>




        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal test">连接测试</button>
                <button class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
                <button class="layui-btn cancle">取消</button>
            </div>
        </div>

</form>

<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    $("input[name='password']").val("");

    var table_name = "db_instance_info";

    layui.use(['form', 'layedit'], function () {
        var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit,
                upload = layui.upload;


        form.on('select(test789)', function(data){
            console.log(data.value);
        })

        init();

        layedit.set({
            uploadImage: {
                url: '/demo/uploadMaterial' //接口url
                , type: 'post' //默认post
            }
        });
        form.render();
        form.on('submit(demo1)', function (data) {
            console.log(data.field)
            $.ajax({
                url: "/dbm/data_source/save",
                //data:data.field,
                type: "post",
                data: {submit_data: JSON.stringify(data.field), resource_name: table_name},
                success: function (result) {
                    if (result.success) {
                        //parent.renovation(data.field);
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                        parent.query();
                        //layer.closeAll();
                    } else {
                        layer.msg(result.message)
                    }
                }
            })
            return false;
        });

    });

    $(".cancle").click(function () {
        window.parent.layer.closeAll();
    })
    $(".test").click(function () {
        $.ajax({
            url: "/dbm/data_source/test",
            type: "post",
            data: {
                username: $('input[name="username"]').val(),
                password: $('input[name="password"]').val(),
                driver_class_name: $('select[name="driver_class_name"]').val(),
                url: $('input[name="url"]').val(),
            },
            success: function (data) {
                if(data.success){
                    layer.msg(data.data);
                }else {
                    layer.msg(data.message);
                    return false;
                }

            }})
    })

    function parseJson(param) {
        var obj = {};
        var keyvalue = [];
        var key = "", value = "";
        var paraString = param.split("&");
        for (var i in paraString) {
            keyvalue = paraString[i].split("=");
            key = keyvalue[0];
            value = keyvalue[1];
            obj[key] = value;
        }
        return obj;
    }

    function init() {
        var url = window.location.search;
        if (url != null && url != "") {
            url = url.substr(1, url.length);
            if (parseJson(url).id != "") {
                $.ajax({
                    url: "/layui/table/detail",
                    data: {id: parseJson(url).id, resource_name: table_name},
                    async: false,
                    success: function (data) {
                        if (data.success) {
                            $.each(data.data, function (name, value) {
                                console.log(name)
                                var t = $("#from1" + ' *[name=' + name + ']');
                                if (t.prop("localName") == "textarea") {
                                    $("textarea[name='" + name + "']").val(value)
                                }
                                if (t.prop("localName") == "input") {
                                    $("input[name='" + name + "']").val(value)
                                }
                                if (name == "logo_image") {
                                    $("#logo_image").prop("src", value);
                                }
                                if (t.prop("localName") == "select") {
                                    $("select[name='" + name + "']").val(value)

                                }

                            })
                        } else {
                            layer.msg(data.message)
                        }
                    }
                })
            }

        }

    }
</script>

</body>
</html>